package com.zhumo.sz.gaode;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhumo.sz.common.http.HttpClientHelper;
import com.zhumo.sz.config.AddressConfig;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.http.client.HttpClient;
import org.apache.http.entity.ContentType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @author jiaqi.zhu
 * @comment 通过高德api解析城市公路信息 不限于城市公路 收费站 高速公路 等信息
 *
 */
public class GaoDeRoadProcessor extends GaoDeProcessor<GaoDeCityRoad> {

    public GaoDeRoadProcessor(String key) {
        super(key);
    }

    @Override
    public List<GaoDeCityRoad> process(String key) {
        HttpClient client = HttpClientHelper.createDefault();
        HashMap<String, Object> poiSearchParams = new HashMap<>();
        ArrayList<GaoDeCityRoad> gaoDeCityRoadCollect = new ArrayList<>();
        String url = "https://restapi.amap.com/v3/place/text";
        poiSearchParams.put("key",key);
        poiSearchParams.put("types","道路名");
        poiSearchParams.put("keyword","城市公路");
        poiSearchParams.put("children","1");
        poiSearchParams.put("extensions","all");
        poiSearchParams.put("city","深圳");
        poiSearchParams.put("offset","25");
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            poiSearchParams.put("page", i);
            JSONObject response = HttpClientHelper.sendJsonGetRequest(url, AddressConfig.ContentTypeJsonHeader,
                    poiSearchParams, ContentType.APPLICATION_JSON, client);
            JSONArray pois = response.getJSONArray("pois");
            System.out.println(pois);
            if (Objects.isNull(pois) || pois.size() == 0){
                break;
            }
            for (Object o : pois) {
                GaoDeCityRoad gaoDeCityRoad = new GaoDeCityRoad();
                JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(o));
                gaoDeCityRoad.setCityCode(jsonObject.getString("citycode"));
                gaoDeCityRoad.setAdname(jsonObject.getString("adname"));
                gaoDeCityRoad.setCityName(jsonObject.getString("cityname"));
                gaoDeCityRoad.setBussinessArea(jsonObject.getString("business_area"));
                gaoDeCityRoad.setName(jsonObject.getString("name"));
                gaoDeCityRoad.setPCode(jsonObject.getString("pcode"));
                gaoDeCityRoad.setPName(jsonObject.getString("pname"));
                gaoDeCityRoad.setType(jsonObject.getString("type"));
                gaoDeCityRoad.setGdId(jsonObject.getString("id"));
                gaoDeCityRoadCollect.add(gaoDeCityRoad);
            }
        }
        return gaoDeCityRoadCollect;
    }

//    public static void main(String[] args) {
//        GaoDeRoadProcessor gaoDeRoadProcessor = new GaoDeRoadProcessor("d4e5f719651859757fa03e09bbc5130b");
//        gaoDeRoadProcessor.process(gaoDeRoadProcessor.key);
//    }
}
